using System.Web.Mvc;
using ExtendedMvc.Common.Diagnostics;
using ExtendedMvc.Html;

namespace ExtendedMvc.Elements {
	public class Hidden : HiddenBase<Hidden> {
		public Hidden(string id, object value) : base(id, value) { }
	}

	public abstract class HiddenBase<T> : Element<T> where T : HiddenBase<T> {
		private string _onChange;
		private object _value;

		protected object value { get { return _value; } }

		protected HiddenBase(string id, object value)
			: base(HtmlTag.Input, id) {
			Check.Argument.IsNotNull(id, "id");
			SetAttribute(HtmlAttribute.Type, HtmlInputType.Hidden);
			Value(value);
		}

		protected virtual T Value(object value) {
			_value = value;
			return (T)this;
		}

		public virtual T OnChange(string value) {
			_onChange = value;
			return (T)this;
		}

		protected override TagRenderMode TagRenderMode { get { return TagRenderMode.SelfClosing; } }

		protected override void Prepare() {
			SetAttribute(HtmlAttribute.Value, value);
			if (!string.IsNullOrWhiteSpace(_onChange)) SetAttribute(HtmlEventAttribute.OnChange, _onChange);
			base.Prepare();
		}
	}
}
